package com.zrc.ke.core;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;

import androidx.annotation.Nullable;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @Description: 代理Activity  使用代理的activity来加载插件里的activity
 * @Author: wangzs
 * @Date: 2022-03-01 12:53
 * @Version:
 */
public class ProxyActivity extends Activity {

    /**
     * 获取到pluginManager里的resource 不用activity的
     *
     * @return
     */
    @Override
    public Resources getResources() {
        return PluginManager.getInstance(this).getResources();
    }

    @Override
    public ClassLoader getClassLoader() {
        return PluginManager.getInstance(this).getDexClassLoader();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String className = intent.getStringExtra("className");
        try {
            Class mPluginActivityClass = getClassLoader().loadClass(className);
            //实例化 插件里的activity
            Constructor constructor = mPluginActivityClass.getConstructor(new Class[]{});
            Object mPluginActivity = constructor.newInstance(new Object[]{});
            invokeMethod(mPluginActivityClass, mPluginActivity, true, "insertAppContent", new Class[]{Activity.class}, new Object[]{this});
            invokeMethod(mPluginActivityClass, mPluginActivity, true, "onCreate", new Class[]{Bundle.class}, new Object[]{new Bundle()});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void invokeMethod(Class localClass, Object activity, Boolean isPublic, String methodName, Class[] parmsClass, Object[] parms) {
        try {
            Method method;
            if (isPublic) {
                method = localClass.getMethod(methodName, parmsClass);
            } else {
                method = localClass.getDeclaredMethod(methodName, parmsClass);
            }
            method.setAccessible(true);
            if (method != null) {
                method.invoke(activity, parms);
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void startActivity(Intent intent) {
        String className = intent.getStringExtra("className");
        if (className != null) {
            Intent proxyIntent = new Intent(this, ProxyActivity.class);
            proxyIntent.putExtra("className", className);
            proxyIntent.putExtras(intent.getExtras());
            super.startActivity(proxyIntent);
        } else {
            super.startActivity(intent);
        }
    }

    @Override
    public ComponentName startService(Intent service) {
        String className = service.getStringExtra("className");
        Intent proxyIntent = new Intent(this, ProxyService.class);
        proxyIntent.putExtra("className", className);
        proxyIntent.putExtras(service.getExtras());
        return super.startService(proxyIntent);
    }

    @Override
    public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter) {
        String name = receiver.getClass().getName();
        ProxyReceiver proxyReceiver = new ProxyReceiver(name);
        return super.registerReceiver(proxyReceiver, filter);
    }

}
